/**
 * related to main.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-09-08
 */
var app = sm("do_App");
var page = sm("do_Page");
var nf = sm("do_Notification");

var qqHttp = require("helper/http");
var deliveryUrl = require("url/delivery");
var common = require("common");
var rootview = ui("$");

var do_LinearLayout_main = ui("do_LinearLayout_main");
var aLayout_deliveryName = ui("aLayout_deliveryName");
var label_deliveryName = ui("label_deliveryName");
var aLayout_addressId = ui("aLayout_addressId");
var label_addressId = ui("label_addressId");
var label_distance = ui("label_distance");
var label_price = ui("label_price");
var button_ok = ui("button_ok");
var textField_deliveryNo = ui("textField_deliveryNo");
var label_serviceName = ui("label_serviceName");
var label_servicePhone = ui("label_servicePhone");
var label_serviceAddress = ui("label_serviceAddress");

//代收地点
var tyj_x = 113.86996098691093;
var tyj_y = 22.62039016053022;

//andriod返回键
page.on("back", function() {
    app.closePage();
});

page.on("result", function(data) {
	if (data && data.fromLogisticsList == 1) { //选择物流公司返回
		label_deliveryName.text = data.name;
	} else if (data.nextAddr || data.houseNumber) { //选择地址返回
		label_addressId.tag = data.id;
		label_addressId.text = data.nextAddr + " " + data.contactName+ " " + data.contactPhone;
		var a = {"lng":data.x,"lat":data.y};
		var b = {"lng":tyj_x,"lat":tyj_y};
		var distance = getDistance(a,b);
		label_distance.text = Math.ceil(distance/1000);
		setPrice(Math.ceil(distance/1000));
	}
});

page.on("loaded", function(){
	qqHttp.post(deliveryUrl.url.param.getDefaultValue, {}, function(data) {
		tyj_x = data.servicex;
		tyj_y = data.servicey;
		label_serviceName.text = data.serviceName;
		label_servicePhone.text = data.servicePhone;
		label_serviceAddress.text = data.serviceAddress;
		if (data && data.userAddress) {
			var userAddress = data.userAddress;
			label_addressId.tag = userAddress.id;
			label_addressId.text = userAddress.nextAddr + " " + userAddress.contactName+ " " + userAddress.contactPhone;
			//桃源居{"longitude":113.86996098691093,"latitude":22.62039016053022}
			var a = {"lng":userAddress.x,"lat":userAddress.y};
			var b = {"lng":tyj_x,"lat":tyj_y};
			var distance = getDistance(a,b);
			label_distance.text = Math.ceil(distance/1000); //0.1也算一公里
			setPrice(Math.ceil(distance/1000));
		}
	});
});

/* 头部 */
var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage()
});
var labelTitle = ui(addheader+".labelTitle");
labelTitle.text = "代收代送";

var headerRight = ui(addheader + ".headerRight");
//右上角添加+图标
var imgNext = ui(addheader + ".imgNext");
imgNext.source = "source://image/common/more.png";
imgNext.visible = true;

/* 广告图 */
var bannerScroll = do_LinearLayout_main.add("crollbanner","source://view/reuse/banner/banner.ui", 0, 0);
var bannerScrollf = ui("crollbanner");
var bimgs = [ {
	"imgs" : "source://image/serice/dsds.jpg",
	"path" : "source://view/delivery/introduce.ui"
},{
	"imgs" : "source://image/serice/dsds.jpg",
	"path" : "source://view/delivery/introduce.ui"
}];
bannerScrollf.fire("bannerSc", bimgs);

aLayout_deliveryName.on("touch", function() {
	common.open("source://view/delivery/logisticsList.ui");
});

aLayout_addressId.on("touch", function() {
	common.open("source://view/user/userAddress/list.ui",{select:true,selectId:label_addressId.tag});
});

button_ok.on("touch", function(data, e){
	var deliveryName = label_deliveryName.text;
	if (deliveryName == '物流公司') {
		nf.toast("请选择物流公司");
		return;
	}
	var deliveryNo = textField_deliveryNo.text.trim();
	var addressId = label_addressId.tag;
	var price = label_price.text;
	var param = {"deliveryName":deliveryName,"deliveryNo":deliveryNo,"addressId":addressId,"price":price};
	qqHttp.post(deliveryUrl.url.order.add, param, function(data) {
		nf.toast("下单成功");
		var orderNo = data.orderNo;
		common.open("source://view/order/detail.ui", {orderNo : orderNo});
	});
});

/* 计算距离 */
function fD(a, b, c) {
	for (; a > c;)
		a -= c - b;
	for (; a < b;)
		a += c - b;
	return a;
};
function jD(a, b, c) {
	b != null && (a = Math.max(a, b));
	c != null && (a = Math.min(a, c));
	return a;
};
function yk(a) {
	return Math.PI * a / 180
};
function Ce(a, b, c, d) {
	var dO = 6370996.81;
	return dO * Math.acos(Math.sin(c) * Math.sin(d) + Math.cos(c) * Math.cos(d) * Math.cos(b - a));
};
function getDistance(a, b) {
	if (!a || !b)
		return 0;
	a.lng = fD(a.lng, -180, 180);
	a.lat = jD(a.lat, -74, 74);
	b.lng = fD(b.lng, -180, 180);
	b.lat = jD(b.lat, -74, 74);
	return Ce(yk(a.lng), yk(b.lng), yk(a.lat), yk(b.lat));
};
/* 计算距离end */

/* 计算价格 */
function setPrice(distance) {
	if (distance < 5) {
		label_price.text = 0;
	} else if (distance < 10) {
		label_price.text = 5;
	} else if (distance < 20) {
		label_price.text = 10;
	} else if (distance < 40) {
		label_price.text = 20;
	} else {
		label_price.text = 100;
	}
}
/* 计算价格end */

/*
 * 菜单弹出
 */
var menus;
var popmenu = rootview.add("menupop", "source://view/reuse/menu/menu.ui", 0, 0);
var popmenuf = ui("menupop");
headerRight.on("touch", function() {
	menus= [{
		"template" : 0,
		"menuIcon" : "source://image/menu/order.png",
		"menuName" : "我的订单"
	}]
	popmenuf.fire("popmenuSJ", menus);
});

popmenuf.on("popmenuItem", function(data) {
	var touchIndex = parseInt(data);
	common.open("source://view/order/list.ui",{orderType:5});//我的订单
});